//
// Copyright (c) 2024 BPer
//
#ifndef BPerAudioDecoderAac_H
#define BPerAudioDecoderAac_H

#include <bperutil/buffer/BPerAudioEncoderBuffer.h>
#include <bperdecoder/BPerAudioDecoder.h>
#include <bperutil/sys/BPerLoadLib.h>
#include <bperutil/BPerAvInfoType.h>
#include <faad.h>
#include <vector>

using namespace std;

class BPerAudioDecoderAac : public BPerAudioDecoder {
public:
    BPerAudioDecoderAac(BPerAudioParam *pcontext);

    ~BPerAudioDecoderAac();

    void init();

    int32_t decode(BPerFrame *pframe, BPerDecoderCallback *pcallback);


protected:

private:

    void closeAacdec();

    int32_t isConvert;
    uint8_t *temp;
    NeAACDecHandle m_handle;

    int32_t isFirst;
    NeAACDecFrameInfo m_info;
    unsigned long m_samplerate;
    uint8_t m_channel;
    unsigned long m_bufLen;
    uint8_t *m_buffer;
    BPerLoadLib m_lib;

    void loadLib();

    void unloadLib();

    NeAACDecHandle (*bPer_NeAACDecOpen)(void);

    NeAACDecConfigurationPtr (*bPer_NeAACDecGetCurrentConfiguration)(NeAACDecHandle hDecoder);

    uint8_t (*bPer_NeAACDecSetConfiguration)(NeAACDecHandle hDecoder,
                                             NeAACDecConfigurationPtr config);

    long (*bPer_NeAACDecInit)(NeAACDecHandle hDecoder,
                              uint8_t *buffer,
                              unsigned long buffer_size,
                              unsigned long *samplerate,
                              uint8_t *channels);

    void *(*bPer_NeAACDecDecode)(NeAACDecHandle hDecoder,
                                 NeAACDecFrameInfo *hInfo,
                                 uint8_t *buffer,
                                 unsigned long buffer_size);

    void (*bPer_NeAACDecClose)(NeAACDecHandle hDecoder);


};

#endif // AACDECODER_H
